package com.key.service.impl;

import com.key.common.R;
import com.key.common.TranException;
import com.key.common.dto.AssetsInfoDTO;
import com.key.entity.AssetsAllocation;
import com.key.mapper.AssetsAllocationMapper;
import com.key.service.AssetTypeService;
import com.key.service.AssetsAllocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @Author: li
 * @CreateTime: 2023-02-22  14:28
 */
@Service
public class AssetsAllocationServiceImpl implements AssetsAllocationService {

    @Autowired
    private AssetsAllocationMapper allocationMapper;

    @Override
    public R showAllAssetsAllocationInfo(AssetsAllocation assetsAllocation) {

        R r = new R();

        try {
            List<AssetsAllocation> assetsAllocationList = allocationMapper.selectAllAllocationInfo(assetsAllocation);
            r.setCode(200);
            r.setMsg("success");
            r.setData(assetsAllocationList);
        } catch (Exception e) {
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }

        return r;
    }

    @Override
    @Transactional
    public R allocationConfirm(Integer[] allocationIds, String inDate) {

        R r = new R();

      //  String[] ids = allocationIds.split(",");
        try {

            int i=0;
            for (Integer id:allocationIds){
                //修改调拨单信息
                System.out.println(id);
                i = allocationMapper.updateAllocationState(id, inDate);
                //查询一张资产调拨单信息，用于修改资产信息
                AssetsAllocation assetsAllocation = allocationMapper.selectAllocationInfoById(id);
                //根据一个调拨单号查询其所属资产id
                List<Map<String,Long>> list = allocationMapper.selectAssetsNumInfoByAllocationId(assetsAllocation.getAllocationNum());
                System.out.println(list);
                //修改资产信息
                for (Map<String,Long> map : list) {
                    System.out.println(map.get("assets_id"));
                    int i1 = allocationMapper.updateAssetsInfoByAllocationId(map.get("assets_id"),assetsAllocation.getInPerson(),assetsAllocation.getInCompany());

                }

            }

            if (i>0) {

                r.setCode(200);
                r.setMsg("调入成功");
                return r;
            }
            else {
                r.setCode(500);
                r.setMsg("调入失败");
                return r;
            }
        } catch (Exception e) {
            throw new TranException("事务异常");
        }



    }

    @Override
    @Transactional
    public R allocationCancel(Integer[] allocationIds) {

        R r = new R();
     //   String[] ids = allocationIds.split(",");
        try {
            int i=0;
            for (Integer id:allocationIds){
                //查询一张资产调拨单信息，用于修改资产信息
                AssetsAllocation assetsAllocation = allocationMapper.selectAllocationInfoById(id);
                //根据一个调拨单号查询其所属资产id
                List<Map<String,Long>> list = allocationMapper.selectAssetsNumInfoByAllocationId(assetsAllocation.getAllocationNum());
                System.out.println(list);
                //修改资产信息
                for (Map<String,Long> map : list) {
                    System.out.println(map.get("assets_id"));
                    int i1 = allocationMapper.updateAssetsInfoByAllocationIdNo(map.get("assets_id"));

                }
                //删除调拨单
                i = allocationMapper.deleteAllocationInfoById(id);

            }

            if (i>0) {

                r.setCode(200);
                r.setMsg("取消调入成功");
                return r;
            }
            else {
                r.setCode(500);
                r.setMsg("取消调入失败");
                return r;
            }
        } catch (Exception e) {
            throw new TranException("事务异常");
        }



    }

    @Override
    public R showAssetsAllocationInfo( Integer allocationId) {
        R r = new R();

        try {
            AssetsAllocation allocation = allocationMapper.selectAllocationInfoById(allocationId);
            r.setCode(200);
            r.setMsg("success");
            r.setData(allocation);
        } catch (Exception e) {
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }

        return r;
    }

    @Override
    public R showAssetsByAllocationNum(String allocationNum) {
        R r = new R();

        try {
            List<AssetsInfoDTO> assetsAllocationList = allocationMapper.selectAssetsByAllocationNum(allocationNum);
            r.setCode(200);
            r.setMsg("success");
            r.setData(assetsAllocationList);
        } catch (Exception e) {
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }
        return r;
    }

    @Override
    public R updateAllocationInfo(Integer allocationId,String inPerson, String inCompany, String description) {
        R r = new R();

        try {
            int i = allocationMapper.updateAllocationInfo(allocationId, inPerson, inCompany, description);
            if (i>0){
                r.setCode(200);
                r.setMsg("修改成功");
            }
            else {
                r.setCode(500);
                r.setMsg("修改失败");
            }


        } catch (Exception e) {
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }
        return r;
    }

    @Override
    public R deleteAllocationInfoById(Integer[] allocationIds) {

        R r = new R();
    //    String[] ids = allocationIds.split(",");

        try {
            int i=0;
            for (Integer id:allocationIds){
                 i = allocationMapper.deleteAllocationInfoById(id);
                 i++;
            }
            if (i>0){
                r.setCode(200);
                r.setMsg("删除成功");
            }
            else {
                r.setCode(500);
                r.setMsg("删除失败");
            }
        } catch (Exception e) {
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }

        return r;

    }
}
